/*
*
*  导航数据模型
*  负责导航部分数据
*
*/
const db = require('../../config/db')
module.exports = {
    // 编写添加导航方法
    // 参数：nav_id，nav_text
    add(nav_id, nav_text,user_id,nav_create_time) {
        // console.log({nav_id, nav_text,user_id,nav_create_time})
        return new Promise((resolve, reject) => {
            db.query("select * from db_navigate where nav_text='" + nav_text + "'", (err, rows) => {
                if (err) {
                    resolve({ code: -1, msg: '查询导航名称失败。' })
                } else {
                    if (rows.length == 0) {
                        db.query("insert into db_navigate(nav_text,nav_id,user_id,nav_create_time) values('" + nav_text + "','" + nav_id + "','" + user_id + "','"+nav_create_time+"')", (err, rows) => {
                            if (err) {
                                resolve({ code: -1, msg: '添加导航失败。' ,err })
                            } else {
                                resolve({ code: 200, msg: '添加导航成功。', nav_text })
                            }
                        })
                    } else {
                        resolve({ code: 201, msg: '导航已存在。' })
                    }
                }
            })
        })
    },
    
    // 编写删除导航方法
    // 参数：nav_id
    remove(nav_id) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_navigate where nav_id='" + nav_id + "'", (err, rows) => {
                if (err) {
                    resolve({ code: -1, msg: '查询导航名称失败。' })
                } else {
                    db.query("delete from  db_navigate where nav_id = '" + nav_id + "'", function (err, rows) {
                        if (err) {
                            resolve({ code: -1, msg: '删除导航失败。' })
                        } else {
                            resolve({ code: 200, msg: '删除导航成功。' })
                        }
                    })
                }
            })
        })
    },

    // 编写查询导航方法
    // 参数：无
    list() {
        return new Promise((resolve, reject) => {
            db.query("select * from db_navigate", (err, rows) => {
                if (err) {
                    // 反馈信息给客户端
                    resolve({ code: -1, msg: '查询导航失败。' })
                } else {
                    resolve({ code: 200, msg: '查询导航成功。', result: rows })
                }
            })
        })
    },

    // 编写修改导航方法
    // 参数：nav_id,nav_text
    modify(nav_id, nav_text) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_navigate where nav_id='" + nav_id + "'", (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询用户失败。'
                    })
                } else {
                    if (rows.length != 0) {
                        db.query("update db_navigate set nav_text = '" + nav_text + "' where  nav_id = '" + nav_id + "'", function (err, rows) {
                            if (err) {
                                resolve({
                                    code: -1,
                                    msg: "修改导航失败。"
                                });
                            } else {
                                resolve({
                                    code: 200,
                                    msg: "修改导航成功。"
                                })
                            }
                        })
                    }
                }
            })
        })
    },

    // 编写获取广告详情的方法
    // 参数：nav_id
    detail(nav_id) {
        return new Promise((resolve, reject) => {
            // 查询语句
            let sql = 'select * from db_navigate where nav_id = "' + nav_id + '"'
            // 调用查询方法
            db.query(sql, function (err, rows) {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询导航失败。'
                    })
                } else {
                    resolve({
                        code: 200,
                        result: rows,
                        msg: '查询导航成功。'
                    });
                }
            });
        })
    },

    
}